
#include <manager/solvermanager.h>
#include <gfx/levelofdetail.h>

ceSolverManager* ceSolverManager::static_instance = 0;

ceSolverManager::ceSolverManager()
  : _defaultEffectSolver(0)
{
  CE_OBJECT_CONSTR
}


ceSolverManager::~ceSolverManager()
{
	std::map<std::string, iLevelOfDetailSolver*>::iterator it;
	for (it = _solvers.begin (); it != _solvers.end (); ++it)
		{
			iLevelOfDetailSolver* solver = it->second;
			solver->Release ();
		}
	_solvers.clear();
}


ceSolverManager* ceSolverManager::Get()
{
	if (!ceSolverManager::static_instance)
		{
			ceSolverManager::static_instance = new ceSolverManager ();
			ceSolverManager::static_instance->AddRef();
		}
	return ceSolverManager::static_instance;
}

ceSolverManager* ceSolverManager::Reset(ceSolverManager *solverManager)
{
	if (!solverManager)
		{
			solverManager = new ceSolverManager ();
		}
  CE_SET(ceSolverManager::static_instance, solverManager);
	return ceSolverManager::static_instance;
}

void ceSolverManager::AddSolver(const std::string &name, iLevelOfDetailSolver *solver)
{
	assert (solver);
  CE_SET(_solvers[name], solver);
}

iLevelOfDetailSolver* ceSolverManager::GetSolver(const std::string &name)
{
	std::map<std::string, iLevelOfDetailSolver*>::iterator it;
	it = _solvers.find (name);
	if (it != _solvers.end ())
		{
			return it->second;
		}

	return 0;
}

const iLevelOfDetailSolver* ceSolverManager::GetSolver(const std::string &name) const
{
	std::map<std::string, iLevelOfDetailSolver*>::const_iterator it;
	it = _solvers.find (name);
	if (it != _solvers.end ())
		{
			return it->second;
		}

	return 0;
}


void ceSolverManager::SetDefaultEffectSolver(iLevelOfDetailSolver *solver)
{
  CE_SET(_defaultEffectSolver, solver);
}

iLevelOfDetailSolver *ceSolverManager::GetDefaultEffectSolver() const
{
  return _defaultEffectSolver;
}

